﻿'''   Copyright 2010 Tom Mealey
'''
'''   Licensed under the Apache License, Version 2.0 (the "License");
'''   you may not use this file except in compliance with the License.
'''   You may obtain a copy of the License at
'''
'''       http://www.apache.org/licenses/LICENSE-2.0

'''   Unless required by applicable law or agreed to in writing, software
'''   distributed under the License is distributed on an "AS IS" BASIS,
'''   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'''   See the License for the specific language governing permissions and
'''   limitations under the License.

Public Class Main
    Private WithEvents Serial As serControl

    Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ports() As serControl.serPort = serControl.GetPorts
        For Each port As serControl.serPort In ports
            Me.lstPorts.Items.Add(port)
        Next
    End Sub

    Private Sub lstPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPorts.SelectedIndexChanged
        If lstPorts.SelectedItems.Count = 1 Then
            Me.btnOpenPort.Enabled = True
        Else
            Me.btnOpenPort.Enabled = False
        End If
    End Sub

    Private Sub btnOpenPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenPort.Click
        If Serial Is Nothing Then
            lstPorts.Enabled = False
            lstPorts.Visible = False
            Dim port As serControl.serPort = lstPorts.SelectedItem
            Serial = New serControl(port.Name)
            Serial.Open()
        End If
    End Sub

    Private Sub Serial_DataRecieved(ByVal bytes As Integer) Handles Serial.DataRecieved
        'Console.WriteLine(Serial.Read(bytes))
        txtDataRecieved.Text = Strings.Right(txtDataRecieved.Text & Serial.Read(bytes), 12000)
    End Sub
End Class